<?php
/**
 * 年龄检查
 * User: GRAY
 * Date: 16/8/11
 * Time: 下午10:23
 */

namespace Home\Common\Hook;


use Common\Library\BusinessException;
use Common\Library\Constant;
use Common\Vendor\Hook\BaseHook;
use Think\Log;


class Location extends BaseHook
{

    public function execute($data, $params = [])
        {
            $locatino_pass = $data['location'];
            if($location_pass==1) {
                return $data;
            }
	    $address = $data['birthplace'];
	    $city = $this->getCityName($address);

	   foreach($params["province_not_include"] as $item){
		   if(strpos($address, $item) === 0){
			   BusinessException::throwException(Constant::INVALID_PROVINCE);
		   }
	   }

	   if(!empty($city)){
	      if(in_array($city , $params["city_not_include"])){
	       	BusinessException::throwException(Constant::INVALID_CITY);
	      }
	   }
	   return $data;
        }

    /**
     * 获取地址中的城市名称
     * 注意：暂时不支持新疆，宁夏等特殊地区
     * @param $address
     * @return bool|string
     */
    private function getCityName($address)
    {
        if (strpos($address, '市') !== false) {
            if (strpos($address, '省') !== false) {
                $province = strstr($address, '省');
                $country = strstr($address, '市');
                $countryName = str_replace([$country, '省'], '', $province) . '市';
            }
            return $countryName;
        }
        return false;
    }

} 
